import javax.swing.tree.TreeNode;
import java.util.*;

public class Test {
    public int[] elem;
    public int usedSize;
    private Object[] queue;


    /**
     * 建堆的时间复杂度：
     *
     * @param array
     */
    public void createHeap(int[] array) {
        for(int root = (usedSize-1-1)/2; root >= 0; root--){
            shiftDown(root,usedSize);
        }

    }

    /**
     *
     * @param root 是每棵子树的根节点的下标
     * @param len  是每棵子树调整结束的结束条件
     * 向下调整的时间复杂度：O(logn)
     */
    private void shiftDown(int root,int len) {
        int child =(2*root)+1;
        while(child<usedSize){
            if(child+1<usedSize && elem[child]<elem[child+1]){
                child++;
            }
            if(elem[child] > elem[root]){
                swap(child,root);
                root = child;
                child = root*2+1;
            }else{
                break;
            }
        }

    }
    public void swap(int root,int child){
        int tmp = root;
        root = child;
        child = tmp;
    }


    /**
     * 入队：仍然要保持是大根堆
     * @param val
     */
    public void push(int val) {
        if(isFull()){
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        this.elem[usedSize] = val;
        shiftUp(usedSize);
        usedSize++;

    }

    private void shiftUp(int child) {
        int parent = (child-1)/2;
        while(child>0){
            if(elem[child] > elem[parent]){
                swap(child,parent);
                child = parent;
                parent = (child-1)/2;
            }else{
                break;
            }
        }

    }

    public boolean isFull() {
        return usedSize == elem.length;

    }

    /**
     * 出队【删除】：每次删除的都是优先级高的元素
     * 仍然要保持是大根堆
     */
    public int pollHeap() {
        int tmp = elem[0];
        swap(0,usedSize-1);
        usedSize--;
        shiftDown(0,usedSize);
        return tmp;
    }

    public boolean isEmpty() {
        return usedSize==0;
    }

    /**
     * 获取堆顶元素
     * @return
     */
    public int peekHeap() {
        if(isEmpty()){
            return -1;
        }
        return elem[0];
    }
      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    public List<List<Integer>> levelOrder (TreeNode root){
        List<List<Integer>> list = new ArrayList<>();
        if(root == null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> tmp = new ArrayList<>();
            while(size != 0){
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                size--;
                if(cur.left != null){
                     queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
             list.add(tmp);
        }
        return list;
    }

    public static void main(String[] args) {
        int[] array = {27,0,8,0,4,2};
        Sort.insertSort(array);
        System.out.println(Arrays.toString(array));
    }


        // 希尔排序
    public static void shellSort(int[] array){
        // write code  here
    }

}
